package com.health.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.health.model.SystemSettings;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

public class SPUtil {
	public static final String SP_NAME = "nova_reminder";

	public static String REGISTER_IMEI = "registerImei";
	public static String REGISTER_ZCMALL = "registerZCMAll";
	public static String REGISTER_XZQ = "registerXZQ";
	public static String IS_SHI_YONG = "isShiYong";
	public static String STR_IDCARD = "str_idCard";
	public static String STR_XZQ = "str_xzq";
	public static String STR_XZ = "str_xz";

	public static String STR_CZ = "str_cz";

	// public static String SYSTEMSETTINGS = "SystemSettings";
	public static String ISBLOOTH2 = "isBlooth2";

	public static String SETTINGS = "sysSettings";
	public static String STR_JDDWNO = "str_jddwno";// 建档单位no
	public static String STR_JDDW = "str_jddw";// 建档单位
	public static String STR_JDRNO = "str_jdrno";// 建档人no
	public static String STR_JDR = "str_jdr";// 建档人
	public static String STR_JDRUSERNAME = "str_jdrUserName";// 建档人名字
	public static String STR_JDPADDWARD = "str_jdpassward";// 建档人密码
	public static String STR_JDRIDCARD = "str_jdrIdcard";// 建档人身份证号


	public static String STR_XZNO = "str_xzno";
	public static String STR_CZNO = "str_czno";
	public static String STR_SSQNO = "str_ssqno";
	public static String STR_P = "str_p";// 省
	public static String STR_C = "str_c";// 市
	public static String STR_N = "str_n";// 县

	public static String STR_UPLOAD_ADDRESS = "str_upload_address";// 公卫数据上传地址
	public static String STR_SHOUQUAN_ADDRESS = "str_shouquan_address";// 授权地址
	public static String STR_UPLOAD_JTYS_ADDRESS = "str_upload_jtys_address";// 家庭医生数据上传地址

	public static String STR_DZJMJKK_ADDRESS = "str_dzjmjkk_address";// 设置电子居民健康卡

	public static String BOOLEAN_DZJMJKK = "boolean_dzjmjkk";// 设置电子居民健康卡
	public static String BOOLEAN_ZYH="boolean_zyh";//设置住院号
	public static String BOOLEAN_XIAOZHAN="boolean_xiaozhan";//

	private static String TOKEN = "token";

	public static void setToken(Context context, String token) {
		saveStr(context, SP_NAME, TOKEN, token);
	}

	public static String getToken(Context context) {
		return SPUtil.getStr(context, SP_NAME, TOKEN);
	}


	public static void setSTR_DZJMJKK_ADDRESS(Context context, String str_upload_address) {
		saveStr(context, SP_NAME, STR_DZJMJKK_ADDRESS, str_upload_address);
	}

	public static String getSTR_DZJMJKK_ADDRESS(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_DZJMJKK_ADDRESS);
	}

	public static void setBOOLEAN_DZJMJKK(Context context, boolean booleanDzjmjkk) {
		saveBoolean(context, SP_NAME, BOOLEAN_DZJMJKK, booleanDzjmjkk);
	}

	public static boolean getBOOLEAN_DZJMJKK(Context context) {
		return SPUtil.getBoolean(context, SP_NAME, BOOLEAN_DZJMJKK);
	}
	public static void setBOOLEAN_ZYH(Context context, boolean booleanZYH) {
		saveBoolean(context, SP_NAME, BOOLEAN_ZYH, booleanZYH);
	}

	public static boolean getBOOLEAN_ZYH(Context context) {
		return SPUtil.getBoolean(context, SP_NAME, BOOLEAN_ZYH);
	}

	public static void setBOOLEAN_XIAOZHAN(Context context, boolean booleanXiaoZhan) {
		saveBoolean(context, SP_NAME, BOOLEAN_XIAOZHAN, booleanXiaoZhan);
	}

	public static boolean getBOOLEAN_XIAOZHAN(Context context) {
		return SPUtil.getBoolean(context, SP_NAME, BOOLEAN_XIAOZHAN);
	}

	public static void setSTR_UPLOAD_ADDRESS(Context context, String str_upload_address) {
		saveStr(context, SP_NAME, STR_UPLOAD_ADDRESS, str_upload_address);
	}

	public static String getSTR_UPLOAD_ADDRESS(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_UPLOAD_ADDRESS);
	}
	public static void setStrShouquanAddress(Context context, String str_shouquan_address) {
		saveStr(context, SP_NAME, STR_SHOUQUAN_ADDRESS, str_shouquan_address);
	}

	public static String getStrShouquanAddress(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_SHOUQUAN_ADDRESS);
	}


	public static void setSTR_UPLOAD_JTYS_ADDRESS(Context context, String str_upload_address) {
		saveStr(context, SP_NAME, STR_UPLOAD_JTYS_ADDRESS, str_upload_address);
	}
	
	public static String getSTR_UPLOAD_JTYS_ADDRESS(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_UPLOAD_JTYS_ADDRESS);
	}

	public static void setstr_xzno(Context context, String str_xzno) {
		saveStr(context, SP_NAME, STR_XZNO, str_xzno);
	}

	public static String getstr_xzno(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_XZNO);
	}

	public static void setstr_czno(Context context, String str_czno) {
		saveStr(context, SP_NAME, STR_CZNO, str_czno);
	}

	public static String getstr_czno(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_CZNO);
	}

	public static void setstr_ssqno(Context context, String str_ssqno) {
		saveStr(context, SP_NAME, STR_SSQNO, str_ssqno);
	}

	public static String getstr_ssqno(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_SSQNO);
	}

	public static void setstr_p(Context context, String str_p) {
		saveStr(context, SP_NAME, STR_P, str_p);
	}

	public static String getstr_p(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_P);
	}

	public static void setstr_c(Context context, String str_c) {
		saveStr(context, SP_NAME, STR_C, str_c);
	}

	public static String getstr_c(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_C);
	}

	public static void setstr_n(Context context, String str_n) {
		saveStr(context, SP_NAME, STR_N, str_n);
	}

	public static String getstr_n(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_N);
	}

	public static void setstr_jdpassward(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_JDPADDWARD, str_xzq);
	}

	public static String getstr_jdpassward(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDPADDWARD);
	}

	public static void setstr_xz(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_XZ, str_xzq);
	}

	public static String getstr_xz(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_XZ);
	}

	public static void setstr_cz(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_CZ, str_xzq);
	}

	public static String getstr_cz(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_CZ);
	}

	public static void setstr_jddw(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_JDDW, str_xzq);
	}

	public static String getstr_jddw(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDDW);
	}

	public static void setstr_jdr(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_JDR, str_xzq);
	}

	public static String getstr_jdr(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDR);
	}

	public static void setStr_jdrno(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_JDRNO, str_xzq);
	}

	public static String getStr_jdrno(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDRNO);
	}

	public static void setStrjddwno(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_JDDWNO, str_xzq);
	}

	public static String getStrjddwno(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDDWNO);
	}

	public static void setRegisterImei(Context context, String str_xzq) {
		saveStr(context, SP_NAME, REGISTER_IMEI, str_xzq);
	}

	public static void setisBlooth2(Context context, boolean isBlooth2) {
		saveBoolean(context, SP_NAME, ISBLOOTH2, isBlooth2);
	}

	public static void setstr_idCard(Context context, String str_idCard) {
		saveStr(context, SP_NAME, STR_IDCARD, str_idCard);
	}

	public static void setstr_xzq(Context context, String str_xzq) {
		saveStr(context, SP_NAME, STR_XZQ, str_xzq);
	}

	// public static void setSystemSettings(Context context, String
	// SystemSettings) {
	// saveStr(context, SP_NAME, SYSTEMSETTINGS, SystemSettings);
	// }

	public static void setstr_jdrusername(Context context, String str_jdrusername) {
		saveStr(context, SP_NAME, STR_JDRUSERNAME, str_jdrusername);
	}

	public static void setstr_jdrIdcard(Context context, String str_jdrusername) {
		saveStr(context, SP_NAME, STR_JDRIDCARD, str_jdrusername);
	}

	public static String getstr_idCard(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_IDCARD);
	}

	public static String getstr_xzq(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_XZQ, "370188");
	}

	// public static String getSystemSettings(Context context) {
	// return SPUtil.getStr(context, SP_NAME, SYSTEMSETTINGS);
	// }
	public static String getstr_jdrIdcard(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDRIDCARD);
	}

	public static String getstr_jdrusername(Context context) {
		return SPUtil.getStr(context, SP_NAME, STR_JDRUSERNAME);
	}

	public static void setRegisterZCMAll(Context context, String registerZCMAll) {
		saveStr(context, SP_NAME, REGISTER_ZCMALL, registerZCMAll);
	}

	public static void setRegisterXZQ(Context context, String registerXZQ) {
		saveStr(context, SP_NAME, REGISTER_XZQ, registerXZQ);
	}

	public static void setIsShiYong(Context context, boolean isShiYong) {
		saveBoolean(context, SP_NAME, IS_SHI_YONG, isShiYong);
	}

	public static String getRegisterZCMAll(Context context) {
		return SPUtil.getStr(context, SP_NAME, REGISTER_ZCMALL);
	}

	public static String getRegisterXZQ(Context context) {
		return SPUtil.getStr(context, SP_NAME, REGISTER_XZQ);
	}

	public static boolean getIsShiYong(Context context) {
		return SPUtil.getBooleanTrue(context, SP_NAME, IS_SHI_YONG);
	}

	public static boolean getisBlooth2(Context context, boolean isBlooth2) {
		return SPUtil.getBoolean(context, SP_NAME, ISBLOOTH2);
	}

	public static String getRegisterImei(Context context) {
		return SPUtil.getStr(context, SP_NAME, REGISTER_IMEI);
	}

	/**
	 * 获取系统设置对象
	 */
	public static void setSysSettings(Context context, SystemSettings sysSettings) {
		if (sysSettings != null) {
			saveDeviceData(context, SETTINGS, sysSettings);
		}
	}

	public static SystemSettings getSysSettings(Context context) {
		return getDeviceData(context, SETTINGS);
	}

	// -------------------------------------------------------------------------------

	public static void getUser() {
	}

	public static void saveStr(Context con, String fileName, String key, String value) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		sp.edit().putString(key, value).commit();
	}

	public static String getStr(Context con, String fileName, String key) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		return sp.getString(key, "");
	}

	public static String getStr(Context con, String fileName, String key, String s) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		return sp.getString(key, s);
	}

	public static void saveBoolean(Context con, String fileName, String key, boolean value) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		sp.edit().putBoolean(key, value).commit();
	}

	/**
	 * 默认返回值是false
	 */
	public static boolean getBoolean(Context con, String fileName, String key) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		return sp.getBoolean(key, false);
	}

	/**
	 * 默认返回值是true
	 */
	public static boolean getBooleanTrue(Context con, String fileName, String key) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		return sp.getBoolean(key, true);
	}

	public static void saveInt(Context con, String fileName, String key, int value) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		sp.edit().putInt(key, value).commit();
	}

	public static int getInt(Context con, String fileName, String key) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		return sp.getInt(key, 0);
	}

	public static void saveFloat(Context con, String fileName, String key, float value) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		sp.edit().putFloat(key, value).commit();
	}

	public static float getFloat(Context con, String fileName, String key) {
		SharedPreferences sp = con.getSharedPreferences(fileName, 0);
		return sp.getFloat(key, 0);
	}

	/**
	 * 将对象储存到sharepreference
	 *
	 * @param key
	 * @param device
	 * @param <T>
	 */
	public static <T> boolean saveDeviceData(Context con, String key, T device) {
		SharedPreferences sp = con.getSharedPreferences(SP_NAME, 0);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try { // Device为自定义类
				// 创建对象输出流，并封装字节流
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			// 将对象写入字节流
			oos.writeObject(device);
			// 将字节流编码成base64的字符串
			String oAuth_Base64 = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
			sp.edit().putString(key, oAuth_Base64).commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 将对象从shareprerence中取出来
	 *
	 * @param key
	 * @param <T>
	 * @return
	 */
	public static <T> T getDeviceData(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(SP_NAME, 0);
		T device = null;
		String productBase64 = sp.getString(key, null);

		if (productBase64 == null) {
			return null;
		}
		// 读取字节
		byte[] base64 = Base64.decode(productBase64.getBytes(), Base64.DEFAULT);

		// 封装到字节流
		ByteArrayInputStream bais = new ByteArrayInputStream(base64);
		try {
			// 再次封装
			ObjectInputStream bis = new ObjectInputStream(bais);

			// 读取对象
			device = (T) bis.readObject();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return device;
	}
}